﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class remote_service_binding : BaseForm
    {
        public remote_service_binding()
            : base()
        {
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CREATE REMOTE SERVICE BINDING [" + tbBinding.Text + "]");
            if (cbAuthor.SelectedIndex > 0)
                sb.AppendLine("   AUTHORIZATION [" + cbAuthor.Text + "]");
            else
                sb.AppendLine("   AUTHORIZATION []");

            sb.AppendLine("   TO SERVICE '" + tbService.Text + "'");

            if (cbUser.SelectedIndex > 0)
                sb.AppendLine("   WITH USER = " + cbUser.Text + "");
            else
                sb.AppendLine("   WITH USER = ");
            if (chAnonymous.Checked)
            {
                sb.AppendLine("   ,ANONYMOUS = ON");
            }
            else
            {
                sb.AppendLine("   ,ANONYMOUS = OFF");
            }

            tbCommand.Text = sb.ToString();
        }

        public remote_service_binding(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbUser.DataSource = Utility.authorizations(dbContext);
            cbUser.DisplayMember = "Name";

            cbAuthor.DataSource = Utility.authorizations(dbContext);
            cbAuthor.DisplayMember = "Name";
        }

        private void tbBinding_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbAuthor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbService_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chAnonymous_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}